<?php

namespace App\Services\Admin\Goods;

use App\Exceptions\ShopException;
use Exception;

class CategoryService extends BaseGoodsService
{

    /**
     * 分类列表
     * @param array $params
     * @return array
     * @throws Exception
     */
    public function list(array $params): array
    {
        // 分类总条数
        $total = $this->getShopCategoryModel()->total($params);

        // 查询分类
        $categoryList = $this->getShopCategoryModel()->list($params, [
            'id', 'title', 'img_url'
        ]);

        $list = [];
        foreach ($categoryList as $item) {
            $list[] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'imgUrl' => $item['img_url']
            ];
        }

        return [
            'total' => $total,
            'list' => $list
        ];
    }

    /**
     * 分类详情
     * @param array $params
     * @return array
     * @throws ShopException
     * @throws Exception
     */
    public function detail(array $params): array
    {
        // 查询分类
        $detail = $this->getShopCategoryModel()->getItemById($params['id'], [
            'id', 'title', 'img_url'
        ]);
        if (empty($detail)) {
            throw new ShopException('分类不存在', ShopException::CODE_2001);
        }

        return [
            'id' => $detail['id'],
            'title' => $detail['title'],
            'imgUrl' => $detail['img_url']
        ];
    }

    /**
     * 创建分类
     * @param array $params
     * @return void
     * @throws Exception
     */
    public function create(array $params)
    {
        $createCategory = [
            'title' => $params['title'],
            'img_url' => $params['imgUrl']
        ];

        $this->getShopCategoryModel()->addItem($createCategory);
    }

    /**
     * 更新分类
     * @return void
     * @throws Exception
     */
    public function update(array $params)
    {
        // 查询分类
        $category = $this->getShopCategoryModel()->getItemById($params['id']);
        if (empty($category)) {
            throw new ShopException('分类不存在', ShopException::CODE_2001);
        }

        $updateCategory = [
            'title' => $params['title'],
            'img_url' => $params['imgUrl']
        ];

        $this->getShopCategoryModel()->updateItemById($params['id'], $updateCategory);
    }

    /**
     * 删除分类
     * @param array $params
     * @return void
     * @throws Exception
     */
    public function delete(array $params)
    {
        // 查询分类
        $category = $this->getShopCategoryModel()->getItemById($params['id']);
        if (empty($category)) {
            throw new ShopException('分类不存在', ShopException::CODE_2001);
        }

        // 查询分类是否在使用
        $sponsorGoods = $this->getShopGoodsModel()->getItemByCategoryId($params['id']);
        if ($sponsorGoods) {
            throw new ShopException('分类已被使用，不能删除', ShopException::CODE_2003);
        }

        $this->getShopCategoryModel()->deleteItemById($params['id']);
    }
}
